<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="疾病" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['diseaseId',{rules: [{ required: true, message: '请选择!', }]}]" placeholder="请选择疾病"
                    @change="updateDiseaseId">
            <a-select-option v-for="d in diseaseList" :key="d.id" :value="d.id">{{ d.diseaseName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="症状" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['symptomId',{rules: [{ required: true, message: '请选择!', }]}]"
                    placeholder="请选择疾病症状">
            <a-select-option v-for="d in symptomsList" :key="d.id" :value="d.id">{{ d.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="人物类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            show-search
            option-filter-prop="children"
            :filter-option="filterOption1"
            mode="multiple"
            v-model="peopleTypeList"
            style="width: 100%"
            placeholder="人物类型"
            @change="updatePeopleType">
            <a-select-option value="1">男-成人</a-select-option>
            <a-select-option value="2">男-儿童</a-select-option>
            <a-select-option value="3">女-成人</a-select-option>
            <a-select-option value="4">女-儿童</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="问题描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['problemDescription']" placeholder="请输入问题描述"></a-input>
        </a-form-item>
        <a-form-item label="问题排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['problemSort']" placeholder="请输入问题排序" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="问题状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['problemStatus']">
            <a-radio :value="0">
              普通问题
            </a-radio>
            <a-radio :value="1">
              初始问题
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="上传类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['uploadType']"
            style="width: 100%"
            placeholder="上传类型" @change="updateUploadType">
            <a-select-option value="0">图片</a-select-option>
            <a-select-option value="1">视频</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="问题图片" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.uploadType == 0">
          <j-upload text="问题图片"
                    :number="5"
                    v-decorator="['imageUrl']"
                    fileType="image"></j-upload>
        </a-form-item>
        <a-form-item label="问题视频" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.uploadType == 1">
          <j-upload text="问题视频"
                    :number="1"
                    v-decorator="['videoUrl']"
                    fileType="file"></j-upload>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'


  export default {
    name: "MedicalDiseaseProblemModal",
    components: {
      JDate,
      JUpload,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: "/medical/medicalDiseaseProblem/add",
          edit: "/medical/medicalDiseaseProblem/edit",
          getListByDepartmentId: "/hospital/medicalDetailsDiseaseInfo/getListInfo",
          getDiseaseSymptoms: "/hospital/medicalDetailsDiseaseInfo/getDiseaseSymptoms",
        },
        peopleTypes: '',
        diseaseList: [],
        symptomsList: [],
        peopleTypeList: [],
      }
    },
    created() {
      this.initIndex();
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.initIndex();
        this.form.resetFields();
        this.peopleTypes = '';
        this.peopleTypeList = [];
        if (record.diseaseId) {
          this.updateDiseaseId(record.diseaseId);
        }
        if (record.peopleTypeList) {
          this.peopleTypeList = record.peopleTypeList;
        }
        if (record.peopleTypes) {
          this.peopleTypes = record.peopleTypes;
        }
        if (record.uploadType != null) {
          record.uploadType = record.uploadType.toString();
        }
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'diseaseId', 'symptomId', 'uploadType','imageUrl','videoUrl', 'problemDescription', 'peopleTypes', 'peopleTypeList', 'problemSort', 'problemStatus', 'delFlag', 'createTime', 'createBy', 'updateTime', 'updateBy'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.peopleTypes = this.peopleTypes;
            console.log("表单提交数据", formData);
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'diseaseId', 'symptomId', 'problemDescription', 'problemSort', 'problemStatus', 'delFlag', 'createTime', 'createBy', 'updateTime', 'updateBy'))
      },
      initIndex() {
        //疾病
        getAction(this.url.getListByDepartmentId).then((res) => {
          if (res.success) {
            this.diseaseList = res.result;
          }
        });
      },
      updateDiseaseId(value) {
        let param = {diseaseId: value}
        getAction(this.url.getDiseaseSymptoms, param).then((res) => {
          if (res.success) {
            this.symptomsList = res.result;
          }
        });
      },
      filterOption1(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      updatePeopleType(value) {
        let ids = "";
        value.forEach(a => {
          ids = ids + a + ",";
        });
        this.peopleTypes = ids;
      },
      updateUploadType(value){
        this.model.uploadType = value;
      }
    }
  }
</script>